<template>
  <div class="app-container">
    <el-form :data="form" inline>
      <!--<el-form-item label="账户类型：" prop="userType">-->
      <!--<el-select v-model="form.userType">-->
      <!--<el-option-->
      <!--v-for="item in consts.ACCOUNT_TYPE"-->
      <!--:label="item.title"-->
      <!--:value="item.value"-->
      <!--:key="item.value"-->
      <!--&gt;</el-option>-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="账户名：" prop="userName">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>
      <el-button class="filter-item" type="primary" icon="el-icon-download" @click="exportExcel">导出</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
    </el-form>
    <!--<div class="tip">-->
    <!--<el-tip>-->
    <!--注：目前只支持平层搜索，不可跨层搜索1-->
    <!--</el-tip>-->
    <!--</div>-->
    <el-table
      border :data="tableData.list"
      show-summary
      :summary-method="getSummaries"
      v-loading="loading" element-loading-text="加载中..." class="el-table-align-center">
      <el-table-column label="账户类型" fixed="left" width="120" align="center">
        <template slot-scope="{row}">
          {{row.userType | toConverter('ACCOUNT_TYPE')}}
        </template>
      </el-table-column>
      <el-table-column label="账户名" align="center">
        <template slot-scope="{row}">
          <el-tag class="pointer" v-if="row.userType === 1"
                  @click.native="openDialog({props: row, dialogName: 'custom-dialog'})">
            {{row.userName}}
          </el-tag>
          <span v-else>{{row.userName}}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="首存数" align="center" prop="firstRechargeNum">-->
      <!--</el-table-column>-->
      <el-table-column label="首存金额" align="center" width="150" prop="firstRechargeSum" sortable>
        <template slot-scope="{row}">
          {{row.firstRechargeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="存款数" align="center" prop="rechargeNum" sortable>
      </el-table-column>
      <el-table-column label="取款数" align="center" prop="withdrawNum" sortable >
      </el-table-column>
      <el-table-column label="存款额度" align="center" width="150" prop="rechargeSum" sortable>
        <template slot-scope="{row}">
          {{row.rechargeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="存款手续费" align="center" prop="rechargeFeeSum" sortable>
        <template slot-scope="{row}">{{row.rechargeFeeSum | currency}}</template>
      </el-table-column>
      <el-table-column label="取款额度" align="center" width="150" prop="withdrawSum" sortable>
        <template slot-scope="{row}">
          {{row.withdrawSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="取款手续费" align="center" prop="withdrawFeeSum" sortable>
        <template slot-scope="{row}">{{row.withdrawFeeSum | currency}}</template>
      </el-table-column>

      <!--新增字段-->
       <el-table-column label="平台做庄总投注额" align="center" width="130" prop="betSum" sortable>
        <template slot-scope="{row}">{{row.betSum | currency}}</template>
      </el-table-column>
      <el-table-column label="平台做庄有效投注额" align="center" width="130" prop="betEffectSum" sortable>
        <template slot-scope="{row}">{{row.betEffectSum | currency}}</template>
      </el-table-column>
      <el-table-column label="平台做庄派彩额" align="center" width="130" prop="prizeSum" sortable>
        <template slot-scope="{row}">{{row.prizeSum | currency}}</template>
      </el-table-column>
       <el-table-column label="平台做庄公司输赢" align="center" width="130" prop="winSum" sortable>
         <template slot-scope="{row}">
          {{ row.winSum | currency}}
        </template>
      </el-table-column>
       <el-table-column label="玩家对战投注额" align="center" width="130" prop="playerBetSum" sortable>
        <template slot-scope="{row}">{{row.playerBetSum | currency}}</template>
      </el-table-column>
      <el-table-column label="玩家对战玩家输赢" align="center" width="130" prop="playerWinSum" sortable>
        <template slot-scope="{row}">{{row.playerWinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="玩家对战公司抽水" align="center" width="130" prop="playerWaterFee" sortable>
        <template slot-scope="{row}">{{row.playerWaterFee | currency}}</template>
      </el-table-column>
      <el-table-column label="公司总盈利" align="center" width="130" prop="totalWin" sortable>
        <template slot-scope="{row}">{{row.totalWin | currency}}</template>
      </el-table-column>
      <!--结束-->
      <!--新版本不需要的-->
      <!-- <el-table-column label="总投注额" align="center" prop="withdrawFeeSum" sortable>
        <template slot-scope="{row}">{{row.bettingTotalSum | currency}}</template>
      </el-table-column>
      <el-table-column label="有效投注额" align="center" width="150" prop="bettingSum" sortable>
        <template slot-scope="{row}">
          {{row.bettingSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="派彩额度" align="center" width="150" prop="prizeSum" sortable>
        <template slot-scope="{row}">
          {{row.prizeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="公司输赢" align="center" width="150" prop="winSum" sortable>
        <template slot-scope="{row}">
          {{-1 * row.winSum | currency}}
        </template>
      </el-table-column> -->
      <el-table-column label="红利（包括加币-计入红利）" align="center" width="150" prop="bonusSum" sortable>
        <template slot-scope="{row}">
          {{row.bonusSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="加币（不计红利）" align="center" width="150" prop="addCoinSum" sortable>
        <template slot-scope="{row}">{{row.addCoinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="减币" align="center" width="150" prop="subCoinSum" sortable>
        <template slot-scope="{row}">{{row.subCoinSum | currency}}</template>
      </el-table-column>

      <el-table-column label="返水" align="center" width="150" prop="rebateSum" sortable>
        <template slot-scope="{row}">
          {{row.rebateSum | currency}}
        </template>
      </el-table-column>
    </el-table>
    <custom-dialog :visible="dialogVisible" returnButtonName="返回" className="thirdListDialog" :callback="closeDialog">
      <div slot="title">&nbsp;</div>
      <third-list v-if="dialogVisible" :row-data="dialogProps"></third-list>
    </custom-dialog>
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="Number(tableData.total)">
      </el-pagination>
    </div>
  </div>
</template>


<script>
import ThirdList from './thirdList';
import {
  reportCompanySubListApi as listApi, exportMemberWin,
} from '@/services/api/report';

import {
  form, grid, dialog, handleDownload,
} from '@/build';

export default {
  name: 'report-company-subList',
  components: { ThirdList },
  props: ['today', 'rowData'],
  mixins: [form, grid, dialog, handleDownload],
  data() {
    const st = `${this.rowData.date} 00:00:00`;
    const et = `${this.rowData.date} 23:59:59`;
    return {
      tableData: [],
      form: {
        userName: undefined,
        // userType: -1,
      },
      filters: {
        stime: new Date(st).getTime(),
        etime: new Date(et).getTime(),
        size: 20,
        page: 1,
        // parentId: 1,
      },
      filterDateType: false,
    };
  },
  methods: {
    submitFilterForm() {
      this.filters = Object.assign({}, this.filters, this.form, { page: 1, size: 20 });
      this.getAll();
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property === 'winSum') {
          //values[0] *= -1;
        }
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return _.currency(prev + curr);
            }
            return _.currency(prev);
          }, 0);
        }
      });

      return sums;
    },
    // 导出
    exportHandle() {
      const {
        stime, etime, userName, page, size, parentId,
      } = this.filters;
      const ajaxData = {
        stime, etime, userName, page, size, parentId,
      };
      const filename = '会员输赢明细报表.csv';
      const header = ['账户类型', '账户名', '首存金额', '存款数', '取款数', '存款额度', '存款手续费', '取款额度', '取款手续费', '平台做庄总投注额', '平台坐庄有效投注额', '平台做庄派彩额', '平台做庄公司输赢', '玩家对战投注额', '玩家对战玩家输赢', '玩家对战公司抽水', '公司总盈利', '红利(包括加币-不计红利)','加币(不计红利)','减币', '返水'];
      const filterField = ['userType', 'userName', 'firstRechargeSum', 'rechargeNum', 'withdrawNum', 'rechargeSum', 'rechargeFeeSum', 'withdrawSum', 'withdrawFeeSum', 'betSum', 'betEffectSum', 'prizeSum', 'winSum', 'playerBetSum', 'playerWinSum', 'playerWaterFee', 'totalWin', 'bonusSum','addCoinSum', 'subCoinSum','rebateSum'];
      exportMemberWin(ajaxData).then(([result, data]) => {
        if (result) {
          const exportData = this.responseFormatter(data.list);
          this.export2Excel(header, filterField, exportData, filename);
        } else {
          this.$message({ message: `导出失败,${data.message}`, type: 'warning' });
        }
      });
    },
    // 格式化导出数据
    responseFormatter(data) {
      data.map((item) => {
        item.userType = item.userType == 0 ? '会员' : item.userType;
         item.userName = item.userName;
        item.firstRechargeSum = _.currency(item.firstRechargeSum);
        item.rechargeSum = _.currency(item.rechargeSum);
        item.rechargeFeeSum = _.currency(item.rechargeFeeSum);
        item.withdrawSum = _.currency(item.withdrawSum);
        item.withdrawFeeSum = _.currency(item.withdrawFeeSum);
        item.betSum = _.currency(item.betSum);
        item.betEffectSum = _.currency(item.betEffectSum);
        item.prizeSum = _.currency(item.prizeSum);
        item.winSum = _.currency(item.winSum);
        item.playerBetSum = _.currency(item.playerBetSum);
        item.playerWinSum = _.currency(item.playerWinSum);
        item.playerWaterFee = _.currency(item.playerWaterFee);
        item.totalWin = _.currency(item.totalWin);
        // item.bettingTotalSum = _.currency(item.bettingTotalSum);
        // item.bettingSum = _.currency(item.bettingSum);
        // item.prizeSum = _.currency(item.prizeSum);
        // item.winSum = _.currency(item.winSum);
        item.bonusSum = _.currency(item.bonusSum);
        item.rebateSum = _.currency(item.rebateSum);
        item.addCoinSum=_.currency(item.addCoinSum);
        item.subCoinSum=_.currency(item.subCoinSum);
      });

      return data;
    },
  },
  mounted() {
    // if (!_.isUndefined(this.rowData)) {
    //   const parentId = this.rowData.uid;
    //   this.filters.parentId = parentId;
    // }
    this.bindGet(listApi, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
    });
    this.getAll();
  },

};
</script>

<style lang="scss">
  .thirdListDialog {
    top: 10px !important;
  }
</style>
